<script setup lang="ts">
import axios from 'axios';
import { onMounted, ref } from 'vue';
import type { AxiosResData, ChannelItem, ChannelResData } from '@/types';
import { useChannelStore } from '@/stores/channel';

const store = useChannelStore();

const channelList = ref<ChannelItem[]>([]);

onMounted(async() => {
  const res = await axios.get<AxiosResData<ChannelResData>>('http://geek.itheima.net/v1_0/channels');
  channelList.value = res.data.data.channels;
});

</script>

<template>
  <div class="channel-nav">
    <nav class="list">
      <a
        class="item"
        :class="{ active : item.id === store.activeChannelId }"
        href="javascript:;" 
        v-for="item in channelList"
        :key="item.id"
        @click="store.changeActiveChannelId(item.id)"
      >
        {{ item.name }}
      </a>
    </nav>
  </div>
</template>